package com.woniuxy.woniubookmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.woniubookmanager.entity.Role;
import com.woniuxy.woniubookmanager.mapper.RoleMapper;
import com.woniuxy.woniubookmanager.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.woniubookmanager.vo.RoleAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wugd
 * @since 2022-11-29 04:03:13
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    RoleMapper roleMapper ;
    @Override
    public Page<Role> getRoleByPage(Integer now, Integer size, String rname) {
        Page<Role> page = new Page<>(now,size);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (rname != null && !rname.trim().equals("")){
            queryWrapper.like("name",rname.trim());
        }
        return roleMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    public void removeRoleById(Long id) {
        //1. 删除关联关系
        roleMapper.deleteManageAndRoleOfRelationByRoleId(id);
        roleMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void roleAuthority(RoleAuthority roleAuthority) {
        //删除 原来的权限
        roleMapper.deletePerByRoleid(roleAuthority.getId());
        //添加新的权限
        roleMapper.insertAuthority(roleAuthority);
    }

    @Override
    public Long[] getRoleIdByMid(Long id) {
        return roleMapper.selectRoleIdByMid(id);
    }
}
